<?php

namespace app\common\model;

use think\Model;

class User extends Model
{
    public function add($data)
    {
        $validate = new \app\common\validate\User();
        if (!$validate->scene('add')->check($data)) {
            return $validate->getError();
        }
        unset($data['conpass']);
        $result = $this->save($data);
        if ($result) {
            return 1;
        }else {
            return '添加会员失败！';
        }
    }

    public function edit($data)
    {
        $validate = new \app\common\validate\User();
        if (!$validate->scene('edit')->check($data)) {
            return $validate->getError();
        }
        $status = $this->where(['id' => $data['id'], 'password' => $data['oldpass']])->find();
        if (!$status) {
            return '原密码不正确！';
        }
        unset($data['oldpass']);
        $result = $this->isUpdate(true)->save($data);
        if ($result) {
            return 1;
        }else {
            return '会员信息没有做任何修改！';
        }
    }

    public function login($data)
    {
        $validate = new \app\common\validate\User();
        if (!$validate->scene('login')->check($data)) {
            return $validate->getError();
        }
        unset($data['verify']);
        $result = $this->where($data)->find();
        if ($result) {
            session('username', $data['username']);
            return 1;
        }else {
            return '用户名或者密码错误！';
        }
    }

    public function register($data)
    {
        $validate = new \app\common\validate\User();
        if (!$validate->scene('register')->check($data)) {
            return $validate->getError();
        }
        unset($data['conpass']);
        $result = $this->save($data);
        if ($result) {
            return 1;
        }else {
            return '注册失败！';
        }
    }
}
